package com.neuedu.hisboot.controller;

import com.neuedu.hisboot.dto.Result;
import com.neuedu.hisboot.entity.Customer;
import com.neuedu.hisboot.service.ICustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jaun
 * @since 2025-07-04
 */
@RestController
@RequestMapping("/customer")
public class CustomerController {

    // Controller 调用 Service, Servie 调用 Mapper
    @Autowired
    private ICustomerService customerService;
    /**
     * 返回所有的注册的用户信息
     * 后续所有的返回都是用 Result<T> 包装
     * Result<int> 返回1
     * Result<String> 返回字符串
     */
    @GetMapping("/list")
    public Result<List<Customer>> getCustomers(){
        List<Customer> customers = customerService.list();
        return Result.success(customers);
    }

    /**
     * 根据id 查询用户信息
     */
    @GetMapping("/{id}")
    public Result<Customer> getCustomerById(@PathVariable("id") Integer customerId){
        Customer customer = customerService.getById(customerId);
        if (customer == null){
            return Result.error(500, "没有对应的用户");
        }
        return Result.success(customer);
    }


    @RequestMapping("/find")
    public  Result<List<Customer>> getCustomersByCon(@RequestParam(required = false) String name, @RequestParam(required = false) String number){
        if(!StringUtils.hasText(name) && !StringUtils.hasText(number)){
            return Result.error(500, "请输入查询参数");
        }

        List<Customer> customers = customerService.getCustomerByNameOrNumber(name, number);
        return Result.success(customers);
    }

    /**
     * 创建新的病人信息
     */
    @PostMapping("/create")
    public Result<Customer> createCustomer(@RequestBody Customer customer) {
        try {
            // 设置默认值
            if (customer.getDelMark() == null) {
                customer.setDelMark(1); // 1表示未删除
            }
            if (customer.getCreateDate() == null) {
                customer.setCreateDate(java.time.LocalDate.now());
            }
            
            boolean success = customerService.save(customer);
            if (success) {
                return Result.success(customer);
            } else {
                return Result.error(500, "创建病人信息失败");
            }
        } catch (Exception e) {
            return Result.error(500, "创建病人信息时发生错误: " + e.getMessage());
        }
    }

    /**
     * 更新病人信息
     */
    @PutMapping("/update")
    public Result<Customer> updateCustomer(@RequestBody Customer customer) {
        try {
            boolean success = customerService.updateById(customer);
            if (success) {
                return Result.success(customer);
            } else {
                return Result.error(500, "更新病人信息失败");
            }
        } catch (Exception e) {
            return Result.error(500, "更新病人信息时发生错误: " + e.getMessage());
        }
    }

    /**
     * 删除病人信息（逻辑删除）
     */
    @DeleteMapping("/{id}")
    public Result<String> deleteCustomer(@PathVariable("id") Integer id) {
        try {
            Customer customer = customerService.getById(id);
            if (customer == null) {
                return Result.error(500, "未找到对应的病人信息");
            }
            
            // 逻辑删除，设置delMark为0
            customer.setDelMark(0);
            boolean success = customerService.updateById(customer);
            if (success) {
                return Result.success("删除成功");
            } else {
                return Result.error(500, "删除病人信息失败");
            }
        } catch (Exception e) {
            return Result.error(500, "删除病人信息时发生错误: " + e.getMessage());
        }
    }

    /**
     * 根据性别查询病人信息
     */
    @GetMapping("/gender/{gender}")
    public Result<List<Customer>> getCustomersByGender(@PathVariable("gender") Integer gender) {
        List<Customer> customers = customerService.getByGender(gender);
        return Result.success(customers);
    }

    /**
     * 根据渠道查询病人信息
     */
    @GetMapping("/channel/{channel}")
    public Result<List<Customer>> getCustomersByChannel(@PathVariable("channel") Integer channel) {
        List<Customer> customers = customerService.getByChannel(channel);
        return Result.success(customers);
    }

}

